/*
 * @Author: 凌宇king
 * @Date: 2022-09-04 16:55:18
 * @LastEditTime: 2022-09-25 14:37:13
 */
//加载express模块
const express = require('express');
//创建web服务器
const app = express();
// 引入连接数据库
require('./database/mongoose')
//导入路由模块
const userRouter = require('./router/userRouter');
//导入路由模块
const addressRouter = require('./router/addressRouter');
//导入路由模块 
const adminRouter = require('./router/adminRouter')
//导入路由模块 
const orderRouter = require('./router/orderRouter');
//设置跨域访问
app.all('*', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});
//加载cors跨域中间件
const cors = require('cors')
// 配置解析body post请求数据
app.use(express.urlencoded({ extended: true }))
app.use(express.json())
//注册cors
app.use(cors())
//注册路由
app.use(userRouter);
app.use(addressRouter)
app.use(adminRouter)
app.use(orderRouter)
// 配置绝对静态资源路径
app.use(express.static('public'))
app.use(express.static(__dirname))
//get访问根目录，返回nihao 
app.get('/test', async (req, res) => {
    if (req.url === '/favicon.ico') {
        return
    }
    res.send({ name: 'pz' })
})
//监听5000端口，即web服务器运行在4000端口 
app.listen(5000, () => { console.log('server running at 5000') })








